# -*- coding: utf-8 -*-
# @Date   : 2020/3/23
# @File   : R39_EC_EnterpriseEnforcementAdministrativePenaltyCheck_2D_CF2.py
# @Author : zhaochen
'''eof
name:申请企业行政处罚金额校验
code:R39_EC_EnterpriseEnforcementAdministrativePenaltyCheck_2D_CF2
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import re
import sys
import datetime


reload(sys)
sys.setdefaultencoding('utf-8')

def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = EC_DSJ_2D["data"].get(table_Name)
        if data is None:
            return None
        elif data == "" or (isinstance(data, list) and len(data) == 0):
            return [{}]
        else:
            return data
    except:
        return None

def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def formatDate(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None

def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """

    return_data = None
    data_value = str(data_value)
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        try:
            # return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d')
            return_data = formatDate(data_value)
        except:
            return_data = None

    return return_data


def R39_EC_EnterpriseEnforcementAdministrativePenaltyCheck_2D_CF2():
    Null = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    EFC = formatData("EFC")
    if EFC is None:
        return u'缺失值'
    elif EFC == [{}]:
        return 0
    else:
        EF04 = EFC[0].get("EF04")    # 行政处罚强制执行单元
        if EF04 is None or EF04 == "":
            return 0
        elif EF04 in Null:
            return u'缺失值'
        else:
            result = set()
            for i in range(len(EF04)):
                EF040J01 = EF04[i].get("EF040J01")   # 处罚金额
                EF040R01 = EF04[i].get("EF040R01")   # 处罚日期
                if convertDataType(EF040R01, 'date_time') is None or not isFloat(EF040J01):
                    result.add(None)
                else:
                    result.add((EF040R01, float(EF040J01)))
            if len(result) == 1 and None in result:
                return u"缺失值"
            else:
                result = list(filter(lambda x:x is not None, list(result)))
                res = 0
                for item in result:
                    res += item[1]
                return res

result = R39_EC_EnterpriseEnforcementAdministrativePenaltyCheck_2D_CF2()

